namespace SlackOff
{
    public partial class MainForm : Form
    {
        private TimeStateContext timeStateContext;
        public MainForm()
        {
            InitializeComponent();

        }

        private void TimeStateContext_Tick(object? sender, EventArgs e)
        {
            this.ShowStatus();
            if (this.timeStateContext.State.Status == Status.WorkingFinish
                || this.timeStateContext.State.Status == Status.SlackOffFinish
                || this.timeStateContext.State.Status == Status.SlackOffTime)
            {
                if (this.WindowState != FormWindowState.Maximized)
                {
                    this.WindowState = FormWindowState.Maximized;
                }
                this.Activate();
            }
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            this.Start();
        }

        public void Start()
        {
            this.timeStateContext.State.Start();
            if (this.timeStateContext.State.Status == Status.WorkingTime)
            {
                this.WindowState = FormWindowState.Minimized;
            }
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            this.timeStateContext.Reset();
        }
        const string ConfigFileName = "config.json";

        private void MainForm_Load(object sender, EventArgs e)
        {
            var options = new SlackOffOptions();
            if (File.Exists(ConfigFileName))
            {
                try
                {
                    using var fs = new FileStream(ConfigFileName, FileMode.Open, FileAccess.Read);
                    options = System.Text.Json.JsonSerializer.Deserialize<SlackOffOptions>(fs) ?? new SlackOffOptions();
                }
                finally
                {

                }
            }

            this.timeStateContext = new TimeStateContext(options, this.timer);
            this.timeStateContext.Tick += this.TimeStateContext_Tick;
            this.ShowStatus();
            this.ResetLayout();

            var assembly = System.Reflection.Assembly.GetExecutingAssembly().GetName();
            this.Text = assembly.Name;
        }

        private void ShowStatus()
        {
            var state = this.timeStateContext.State;
            this.lblTime.Text = state.GetDescription();
            this.btnStart.Enabled = state.CanStart;
            this.btnStart.Text = state.StartButtonText;
            this.btnDelay.Enabled = state.CanDelay;
            this.btnDelay.Text = state.DelayButtonText;
            this.notifyIcon.Text = $"{this.Text} {this.lblTime.Text}";
            this.mnuStart.Text = this.btnStart.Text;
            this.mnuStart.Enabled = this.btnStart.Enabled;
            this.mnuDelay.Text = this.btnDelay.Text;
            this.mnuDelay.Enabled = this.btnDelay.Enabled;
        }



        private void MainForm_SizeChanged(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.ShowInTaskbar = false;
            }
            else
            {
                this.ShowInTaskbar = true;
            }
            this.ResetLayout();
        }

        private void ResetLayout()
        {
            this.panel1.Left = (this.Width - this.panel1.Width) / 2;
            this.panel1.Top = (this.Height - this.panel1.Height - 70) / 2;
        }

        private bool isExit = false;
        private void mnuExit_Click(object sender, EventArgs e)
        {
            this.isExit = true;
            this.Close();
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                if (!this.isExit)
                {
                    e.Cancel = true;
                    this.WindowState = FormWindowState.Minimized;
                }
            }
        }

        private void notifyIcon_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (this.WindowState == FormWindowState.Minimized)
                {
                    this.WindowState = FormWindowState.Normal;
                    this.Activate();
                }
                else
                {
                    this.WindowState = FormWindowState.Minimized;
                }
            }
        }

        private void btnDelay_Click(object sender, EventArgs e)
        {
            this.timeStateContext.State.Delay();

        }
    }
}